//
//  UIDatePickerView.m
//  Channel
//
//  Created by liusanchun on 14-3-26.
//  Copyright (c) 2014年 tydic. All rights reserved.
//

#import "UIDatePickerView.h"

@implementation UIDatePickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.cornerRadius = 4;
        
        self.view_title = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
        
        UILabel *ine = [[UILabel alloc]initWithFrame:CGRectMake(5, 45, self.frame.size.width-10, 1)];
        ine.backgroundColor = UIColorFromRGB(0xA4EF8B);
        [self.view_title addSubview:ine];
        
        
        
        self.label_time = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 45)];
        self.label_time.font = [UIFont systemFontOfSize:18];
        NSDate *today = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
        NSString *date =  [dateFormatter stringFromDate:today];
        
        self.label_time.text = date;
        
        
        [self.view_title addSubview:self.label_time];
        [self.view_title addSubview:self.lanel_title];
        
        
        [self addSubview:self.view_title];
        
        
        self.datePicker = [[UIDatePicker alloc] init];
        self.datePicker.frame =CGRectMake(0.0f,50,SCRE_WIDTH-30, self.frame.size.height+10);
        self.datePicker.datePickerMode = _datePickerMode?_datePickerMode: UIDatePickerModeDate;
        [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
        self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"Chinese"];
        

        
        [self addSubview:self.datePicker];
        
        UILabel *ines = [[UILabel alloc]initWithFrame:CGRectMake(5, self.datePicker.frame.size.height+self.datePicker.frame.origin.y, self.frame.size.width-10, 1)];
        ines.backgroundColor = UIColorFromRGB(0xEDEEEF);
        [self addSubview:ines];
        
        
        UIButton *btn_determine = [[UIButton alloc]initWithFrame:CGRectMake(0,self.datePicker.frame.size.height+self.datePicker.frame.origin.y, SCRE_WIDTH-30, 45)];
        [btn_determine setTitle:@"完成" forState:UIControlStateNormal];
        btn_determine.titleLabel.font = [UIFont systemFontOfSize:16];        
        
        [btn_determine setTitleColor:UIColorFromRGB(0x747577) forState:UIControlStateNormal];
        [btn_determine addTarget:self action:@selector(finish:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn_determine];
        
        self.datePickerViewBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, SCRE_HEIGHT)];
        self.datePickerViewBg.backgroundColor = [UIColor blackColor];
        self.datePickerViewBg.layer.opacity = 0.4;
        
        [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self.datePickerViewBg];
    }
    return self;
}
-(void)dateChanged:(id)sender{
    NSDate *select = [self.datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *date =  [dateFormatter stringFromDate:select];
    self.label_time.text = date;
}
- (void)finish:(UIButton*)sender
{
    [self down];
    NSDate *select = [self.datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *date =  [dateFormatter stringFromDate:select];
    
    
    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
    [dateFormatters setDateFormat:@"yyyy-MM-dd"];
    NSString *dates =  [dateFormatters stringFromDate:select];
    
    
    NSMutableDictionary *datadic = [[NSMutableDictionary alloc]init];
    [datadic setObject:date forKey:@"date"];
    [datadic setObject:dates forKey:@"dates"];
    
    
    
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyyMMddHHmmss"];
    
    
    if ([self.delegate respondsToSelector:@selector(didSelectDate:Date:)]) {
        [self.delegate didSelectDate:self
                                Date:datadic];
    }
}

-(void)up{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度，单位秒，浮点数
    [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    self.frame = CGRectMake(15,SCRE_HEIGHT/4,self.frame.size.width, 290);
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}

-(void)down{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度，单位秒，浮点数
    self.frame = CGRectMake(0,self.frame.size.height, self.frame.size.width, 290);
    //    self.view_title.frame = CGRectMake(0,ScreenHeight, ScreenWidth, 45);
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
    [self.datePickerViewBg removeFromSuperview];
    [self removeFromSuperview];
    
}



-(void)animationFinished{
    NSLog(@"动画结束!");
}



-(void)hideTabelView{
    [self down];
    
    [self removeFromSuperview];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
